<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML5本地存储之Web Storage篇<</title>
</head>
<body>
<div style="border: 2px dashed #ccc;width:320px;text-align:center;">
    <label for="sitename">网站名(key)：</label>
    <input type="text" id="sitename" name="sitename" class="text"/>
    <br/>
    <label for="siteurl">网 址(value)：</label>
    <input type="text" id="siteurl" name="siteurl"/>
    <br/>
    <input type="button" onclick="save()" value="新增记录"/>
    <hr/>
    <label for="search_site">输入网站名：</label>
    <input type="text" id="search_site" name="search_site"/>
    <input type="button" onclick="find()" value="查找网站"/>
    <p id="find_result"><br/></p>

</div>
<div>

</div>

<br/>
<div id="list">
</div>
<dl>
    <dt class="background: ">标题</dt>
    <dd style="background: red;">1</dd>
    <dd style="background: aqua">2</dd>
</dl>
<script src="../../js/jquery-1.11.3.min.js"></script>
<script>
    loadAll();
    function save(){
        var siteurl = document.getElementById("siteurl").value;
        var sitename = document.getElementById("sitename").value;
        localStorage.setItem(sitename,siteurl);
        alert("添加成功");
    }

    function find(){
        var serach_site = document.getElementById("search_site").value;
        var sitename = localStorage.getItem(search_site);
        var find_result  = document.getElementById("find_result");
        find_result.innerHTML =serach_site + "的网址是："+ sitename;
    }

    //将所有存储在localstroage钟的对象提取出来，并展现在界面上
    function  loadAll() {
        var list = document.getElementById("list");
        if(localStorage.length > 0){
            var result = "<table border='1'>";
            result += "<tr><td>网站名</td><td>网址</td></tr>"
            for(var i = 0; i < localStorage.length;i++){
                var sitename = localStorage.key(i);
                var siteurl  = localStorage.getItem(sitename);
                result += "<tr><td>" + sitename + "</td><td>" + siteurl +"</td></tr>";

            }
            result += "</table>"
            list.innerHTML  = result;
        }else{
            list.innerHTML = "数据为空。。。"
        }
    }

</script>
</body>
</html>